import { createRouter, createWebHashHistory } from 'vue-router';
import axios from 'axios';
import VueCookie from 'vue-cookie';

import { showWarning } from '@/hooks/useMyMessageBox';

import NormalRouterView from '../views/NormalRouterView.vue';

import MyCarsView from '../views/MyCarsView.vue';
import CheckCarsView from '../views/CheckCarsView.vue';
import WbApplysView from '../views/WbApplysView.vue';

import SpPersonMaintain from '../views/SpPersonMaintain.vue';

import MyApplysView from '../views/MyApplysView.vue';

const routes = [
  {
    path: '/',
    name: 'home',
    component: NormalRouterView,
    redirect: to => {
      // 方法接收目标路由作为参数
      // return 重定向的字符串路径/路径对象
      return { path: '/insideApply/myCars', };
    },
  },
  {
    path: "/insideApply",
    name: "insideApply",
    component: NormalRouterView,
    meta: {
      routers: [
        {
          link: '/insideApply/myCars',
          text: '我的车辆',
        },
        {
          link: '/insideApply/checkCars',
          text: '车辆审批',
        },
        {
          link: '/insideApply/wbApplys',
          text: '外包车辆维护',
        },
        {
          link: '/insideApply/spPersonMaintain',
          text: '审批人维护',
        },
      ],
    },
    children: [
      {
        path: "/insideApply/myCars",
        name: "myCars",
        component: MyCarsView,
      },
      {
        path: "/insideApply/checkCars",
        name: "checkCars",
        component: CheckCarsView,
      },
      {
        path: "/insideApply/wbApplys",
        name: "wbApplys",
        component: WbApplysView,
      },
      {
        path: "/insideApply/spPersonMaintain",
        name: "spPersonMaintain",
        component: SpPersonMaintain,
      },
    ],
  },
  {
    path: "/outsideApply",
    name: "outsideApply",
    component: NormalRouterView,
    meta: {
      routers: [
        {
          link: '/outsideApply/myApplys',
          text: '我的申请',
        },
        // {
        //   link: '/outsideApply/checkCars',
        //   text: '车辆审批',
        // },
        // {
        //   link: '/outsideApply/spPersonMaintain',
        //   text: '审批人维护',
        // },
      ],
    },
    children: [
      {
        path: "/outsideApply/myApplys",
        name: "myApplys",
        component: MyApplysView,
      },
      // {
      //   path: "/insideApply/checkCars",
      //   name: "checkCars",
      //   component: CheckCarsView,
      // },
      // {
      //   path: "/insideApply/spPersonMaintain",
      //   name: "spPersonMaintain",
      //   component: SpPersonMaintain,
      // },
    ],
  },
];

let adminUsers = ['14602', '8715', '3698'];
let limitedPaths = {
  adminLimits: ['/insideApply/spPersonMaintain'],
  checkLimits: ['/insideApply/spPersonMaintain'],
};

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach(async (to, from, next) => {
  let userid = VueCookie.get("userid") ? VueCookie.get("userid") : "";
  if (!userid) {
    if (window.location.hostname == "localhost") {
      VueCookie.set("userid", "14602");
      userid = "14602";
    }
    else {
      userid = await getUser();
    }
  }
  if (!adminUsers.includes(userid)) {
    if (limitedPaths.adminLimits.includes(to.path)) {
      showWarning('您没有权限进入该页面');
      next(false);
    }
    else {
      next();
    }
  }
  else {
    next();
  }

});

function getUser () {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url: '/qdmes/qdmes/LogInMES',
      data: {
        usercode: 'TEST01',
        password: 'Htds@123456',
      },
      dataType: "text",//返回的数据类型
    }).then(res => {
      if (res.data.length) {
        VueCookie.set('userid', res.data[0]['EMP_ID']);
        VueCookie.set('username', res.data[0]['EMP_NAME']);
        VueCookie.set('user_key', res.data[0]['KEY']);
      }
      resolve(res.data[0]['EMP_ID']);
    }).catch(err => {
      console.log('err :>> ', err);
      reject('');
    });
  });
}
// getUser();
export default router;
